/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-03-05 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.example.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.LinkedList;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import org.mizartools.dli.DliException;
import org.mizartools.dli.utility.ItemFactory;
import org.mizartools.system.Article;
import org.mizartools.system.ArticleFactory;
import org.mizartools.system.utility.DefinientiaSignature;
import org.mizartools.system.utility.UniqueIdentifier;
import org.mizartools.system.utility.UniqueIdentifierException;
import org.mizartools.system.utility.UniqueIdentifierType;
import org.mizartools.system.xml.ArticleID;
import org.mizartools.system.xml.Definiens;
import org.mizartools.system.xml.Typ;
import org.mizartools.utility.Html;

@SuppressWarnings("serial")
public class DefinientiaFrame extends JInternalFrame implements Runnable {

	private JTextPane textPane;
	private JPanel panel;
	private JScrollPane scroller;
	private JProgressBar progressBar;
	private String articleID;
	
	public DefinientiaFrame(String aid) {
    	super("Definientia - " + aid, true, true, true, true);
    	this.articleID = aid;
    	panel = new JPanel();
    	panel.setLayout(new BorderLayout());
		panel.setPreferredSize(new Dimension(500, 350));
		textPane = new JTextPane();
        scroller = new JScrollPane();
		scroller.getViewport().add(textPane);
		panel.add(scroller, BorderLayout.CENTER);
		getContentPane().add(panel, BorderLayout.CENTER);
		progressBar = new JProgressBar();
		pack();
	}
	
	private String getHtml() {
    	StringBuilder html = new StringBuilder();
    	html.append("<html>");
    	html.append("<body>");
    	html.append("<table width=100% border=1>");
		html.append("<tr>");
		html.append("<td>ARTICLEID</td>");
		html.append("<td>Signature</td>");
		html.append("</tr>");
    	Article article = ArticleFactory.getInstance().getArticle(articleID);
    	StringBuilder stringBuilder;
		if (article.hasDefinientia()) {
			html.append("<tr>");
    		html.append("<td>");
        	html.append("<font face=\"monospace\"><b>");
        	html.append(article.getAid());
        	html.append("</b></font");
    		html.append("</td>");
    		stringBuilder = new StringBuilder();
    		for (ArticleID articleID : article.getDefinientia().getSignature().getArticleIDList()){
    			if (stringBuilder.length()>0) stringBuilder.append(", ");
    			stringBuilder.append(articleID.getName());
    		}
			html.append("<td>" + stringBuilder.toString() + "</td>");
			html.append("</tr>");
		}
    	html.append("</table>");

    	LinkedList<Definiens> definiensList = article.getDefinientia().getDefiniensList();
    	progressBar.setMaximum(definiensList.size()+1);
    	progressBar.setValue(0);
    	progressBar.setIndeterminate(false);
		html.append("<h2>List of definiens</h2>");
    	
    	html.append("<table width=100% border=1>");
		html.append("<tr>");
		html.append("<td>UniqueIdentifier</td>");
		html.append("<td>Nr</td>");
		html.append("<td>Constrkind</td>");
		html.append("<td>Constrnr</td>");
		html.append("<td>Relnr</td>");
		html.append("<td>Defnr</td>");
		html.append("<td>DefMeaning</td>");
		html.append("<td>Essentials</td>");
		html.append("<td>Formula</td>");
		html.append("<td>typList()</td>");
		html.append("</tr>");

		DefinientiaSignature definientiaSignature = article.getDefinientiaSignature();
    	int i = 0;
		int relativeNr = 0;
		int nrDefiniens = 0;
		for (Definiens definiens : definiensList ){
			nrDefiniens++;
			relativeNr = definiens.getDefnr() - nrDefiniens;
	    	progressBar.setValue(i++);
	    	progressBar.repaint();
			html.append("<tr>");
			html.append("<td><b>");
			try {
				html.append(UniqueIdentifier.getInstance(article.getAid(), UniqueIdentifierType.dfs, definiens.getDefnr() - relativeNr).toString());
			} catch (UniqueIdentifierException e) {}
			html.append("</b></td>");
			html.append("<td>");
			if (definiens.getNr() != null)
				html.append(definiens.getNr().toString());
			html.append("</td>");
			html.append("<td>");
			if (definiens.getConstrkind() != null)
				html.append(definiens.getConstrkind().toString());
			html.append("</td>");
			html.append("<td>");
			if (definiens.getConstrnr() != null)
				html.append(definiens.getConstrnr().toString());
			html.append("</td>");
			html.append("<td>");
			if (definiens.getRelnr() != null)
				html.append(definiens.getRelnr().toString());
			html.append("</td>");
			html.append("<td>");
			if (definiens.getDefnr() != null)
				html.append(definiens.getDefnr().toString());
			html.append("</td>");
			html.append("<td>");
			if (definiens.getDefMeaning() != null) 
				html.append(Html.changeChars(definiens.getDefMeaning().getXMLElementList().toString()));
			html.append("</td>");
			html.append("<td>");
			if (definiens.getEssentials() != null) 
				html.append(Html.changeChars(definiens.getEssentials().getXMLElementList().toString()));
			html.append("</td>");
			html.append("<td>");
			if (definiens.getFormula() != null) 
				html.append(Html.changeChars(definiens.getFormula().getXMLElementList().toString()));
			html.append("</td>");
			html.append("<td>");
			stringBuilder = new StringBuilder();
			if (definiens.getTypList() != null) 
	    		for (Typ typ : definiens.getTypList()){
	    			if (stringBuilder.length()>0) stringBuilder.append(", ");
	    			stringBuilder.append(typ.getXMLElementList().toString());
	    		}
				html.append(Html.changeChars(stringBuilder.toString()));
			html.append("</td>");
			html.append("</tr>");
		}
		html.append("</table>");

		html.append("<h2>List of definiens dli format</h2>");
    	
    	html.append("<table width=100% border=1>");
		html.append("<tr>");
		html.append("<td>UniqueIdentifier</td>");
		html.append("<td>UniqueId</td>");
		html.append("<td>Dli</td>");
		html.append("</tr>");
    	
    	i = 0;
		relativeNr = 0;
		nrDefiniens = 0;
		for (Definiens definiens : definiensList ){
			nrDefiniens++;
			relativeNr = definiens.getDefnr() - nrDefiniens;
	    	progressBar.setValue(i++);
	    	progressBar.repaint();
			html.append("<tr>");
			try {
				html.append("<td><b>");
				html.append(UniqueIdentifier.getInstance(article.getAid(), UniqueIdentifierType.dfs, definiens.getDefnr() - relativeNr).toString());
				html.append("</b></td>");
				html.append("<td>");
				html.append(UniqueIdentifier.getInstance(article.getAid(), UniqueIdentifierType.dfs, definiens.getDefnr() - relativeNr).id);
				html.append("</td>");
			} catch (UniqueIdentifierException e) {}
			html.append("<td>");
			try {
				html.append(ItemFactory.getItem(definientiaSignature, definiens, relativeNr).toString());
			} catch (DliException e) {
				html.append(e.toString());
			}
			html.append("</td>");

			html.append("</tr>");
		}
		html.append("</table>");
		
		
		html.append("</body>");
    	html.append("</html>");
    	return html.toString();
	}

	@Override
	public void run() {
    	progressBar.setIndeterminate(true);
    	getContentPane().add(progressBar, BorderLayout.SOUTH);
		panel.setVisible(false);
		textPane.setContentType("text/html");
		textPane.setText(getHtml());
    	progressBar.setIndeterminate(true);
		textPane.setEditable(false);
		getContentPane().remove(progressBar);
		panel.setVisible(true);
	}
}
